<template>
    <el-container>
        <!--header -->
        <el-header>
            <div>
                <img src="../assets/logo_home.png"/>
                <span>后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <el-container>
            <!--左侧 菜单-->
            <el-aside :width="isCollapse == true ? '64px' : '200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!-- :unique-opened="true"->只允许展开一个菜单 -->
                <!-- :collapse-transition="false" -> 关闭动画 -->
                <!-- router -> 导航开启路由模式 -->
                <!--default-active	当前激活菜单的 index-->
                <!--background-color 菜单 背景色 text-color active-text-color  菜单激活颜色-->

                <el-menu unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active = "activePath"
                        class="el-menu-vertical-demo" background-color="#333744" text-color="#fff" active-text-color="#409FFF">
                    <!--一级菜单的模板区域-->
                    <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
                        <template slot="title">
                            <i :class="iconObj[item.id]"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!--二级菜单-->
                        <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{subItem.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>

                </el-menu>
            </el-aside>
            <!-- 内容主体 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>

    </el-container>
</template>

<script>
    export default {
        name: "Index",
        data(){
            return{
                menuList:[],
                // 是否水平折叠收起菜单
                isCollapse:false,
                // 左侧 菜单 logo
                iconObj: {
                    '125': 'el-icon-user-solid',
                    '103': 'el-icon-collection',
                    '101': 'el-icon-sell',
                    '102': 'el-icon-notebook-2',
                    '145': 'el-icon-data-line'
                },
                // 被激活导航地址
                activePath:''
            }
        },
        created() {
          this.getMenuList()
            this.activePath = window.sessionStorage.getItem("activePath")
        },
        methods:{
            logout(){
                //console.log("注销")
                // 清除 token
                window.sessionStorage.clear()
                //路由 跳转 login
                this.$router.push("/login")
            },
            // 获取请求菜单
            async getMenuList(){
               const {data:res} = await this.$http.get('menus')
                //console.log(res)
                if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
                this.menuList = res.data
                //console.log(res)
            },
            //折叠 展开
            toggleCollapse(){
                //console.log("折叠展开");
                this.isCollapse = !this.isCollapse
            },
            //保存 连接 激活的地址
            saveNavState(val){
                //console.log(val)
                window.sessionStorage.setItem("activePath",val)
            }
        }
    }
</script>

<style lang="less" scoped>
    .el-container{
        height: 100%;
    }
    .el-header{
        background-color: #373f41;
        display: flex;
        justify-content: space-between;
        padding-left: 0;
        color: #fff;
        font-size: 20px;
    >div{
            display: flex;
            align-items: center;
        img{
            height: 40px;
        }
        span{
            padding-left: 20px;
        }
        }

    }
    .el-aside{
        background-color: #333744;
        .el-menu{
            border: none;

        }
    }
    .el-main {
        background-color: #eaedf1;
    }
    .toggle-button{
        background-color: #4A5064;
        font-size: 10px;
        line-height: 24px;
        text-align: center;
        color: #fff;
        letter-spacing: 0.2em;
        cursor: pointer;
    }
</style>